package model;

/**
 * A WebsiteVisit represents the event of visiting a certain website
 * at a specific time. These events form the basis of a model of
 * website browsing history.
 * @author Stephen Hunt, Niel Lebeck
 */
public class WebsiteVisit {
    private static final int SECONDS_PER_DAY = 86400;

    private String myURL;
    private Time myTimestamp;
    private boolean myActive;

    /**
     * Constructs a Website visit with a URL, timestamp, activity marker, and a
     * pointer to the next WebsiteVisit.
     * @param data string array of pertinent data
     * @throws WebHistoryDataException if data is impropely formatted.
     */
    public WebsiteVisit (String[] data) throws WebHistoryDataException {
        if (data[0] == null || data[2] == null) {
            throw new WebHistoryDataException(
                    "Website visit data formatted incorrectly");
        }
        myURL = data[0];
        myTimestamp = new Time(data[2]);
        myActive = true;
    }

    /**
     * Returns URL for this WebsiteVisit.
     * @return This WebsiteVisit's URL.
     */
    public String getURL () {
        return myURL;
    }

    /**
     * Returns whether this WebsiteVisit is active.
     * @return true if this WebsiteVisit is active.
     */
    public boolean isActive () {
        return myActive;
    }

    /**
     * Returns Timestamp for this WebsiteVisit.
     * @return This WebsiteVisit's Timstamp
     */
    public Time getTimestamp () {
        return myTimestamp;
    }

    /**
     * Calculates the length of the visit in seconds.
     * @param nextVisit WebsiteVisit following this WebsiteVisit in history.
     * @return duration of visit to website
     */
    public long getVisitDuration (WebsiteVisit nextVisit) {
        if (nextVisit == null) {
            return 0;
        }
        Time timeDiff = nextVisit.getTimestamp().getDiff(this.getTimestamp());
        long dateSecondsDiff = timeDiff.getDate() * SECONDS_PER_DAY;
        long timeSecondsDiff = timeDiff.getTime();
        return dateSecondsDiff + timeSecondsDiff;
    }

    @Override
    public boolean equals (Object other) {
        if ((((WebsiteVisit) other).getTimestamp() == myTimestamp) &&
                ((WebsiteVisit) other).getURL().equals(myURL)) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    /**
     * Sets the WebsiteVisit to active or inactive.
     * @param setter Whether the WebsiteVisit should be active
     */
    public void setActive (boolean setter) {
        myActive = setter;
    }
}
